<?php
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/13 14:58
 * Desc:
 */

class Service_Api_Special extends Contract_Api_Special
{

    public function get_list($params): array
    {
        /**
         * @var $page
         * @var $pagesize
         */
        extract($params);

        $offset = St_Page::offset($page, $pagesize);

        $model = DB::select('id', 'title', 'm_banner', 'addtime')
            ->from('zt')
            ->where('ishidden', '=', 0);
        if($params['keyword']){
            $model->where('title', 'like', '%'.$params['keyword'].'%');
        }
        $data = $model->order_by(DB::expr('ifnull(displayorder,9999)'), 'asc')
        ->offset($offset)
        ->limit($pagesize)
        ->execute()
        ->as_array();

        foreach ($data as &$item) {
            $item['m_banner'] = $this->img_url($item['m_banner']);
            $item['addtime']  = date('Y-m-d H:i:s', $item['addtime']);
        }

        return $data;
    }

    public function get_detail($id): array
    {
        //专题
        $special = DB::select(
            'id', 'title', 'm_banner', 'addtime', 'introduce', 'm_banner', 'bgcolor',
            'm_bgimage', 'seotitle', 'keyword', 'description', 'm_bgrepeat'
        )
            ->from('zt')
            ->where('ishidden', '=', 0)
            ->where('id', '=', $id)
            ->execute()
            ->current();
        if (!$special) {
            throw new Exception('专题不存在');
        }

        //栏目
        $channels = DB::select('id', 'kindtype', 'title', 'introduce', 'moreurl')
            ->from('zt_channel')
            ->where('ztid', '=', $special['id'])
            ->and_where('isopen', '=', 1)
            ->order_by('displayorder', 'asc')
            ->execute()
            ->as_array();
        if (empty($channels)) {
            throw new Exception('专题栏目不存在');
        }

        $special['introduce'] = $this->content_add_domain($special['introduce']);
        $special['m_banner']  = $special['m_banner']?$this->img_url($special['m_banner']):'';
        $special['m_bgimage'] = $special['m_bgimage']?$this->img_url($special['m_bgimage']):'';

        $api_special_class = new Model_Api_V2_Special();
        foreach ($channels as & $channel) {
            $channel ['introduce'] = (new Model_Api_V2_Xcx())->filter_content($channel ['introduce'] );
            switch ($channel['kindtype']) {
                case 1://优惠券
                    $channel['childs'] = $api_special_class->get_coupon($channel['id']);
                    break;
                case 2://产品
                    $channel['childs'] = $api_special_class->get_product($channel['id']);
                    break;
                case 3://文章
                    $channel['childs'] = $api_special_class->get_article($channel['id']);
                    break;
                default:
            }
        }
        return array('special' => $special, 'channel' => $channels);
    }
}